$(()=>{
    if(sessionStorage.getItem("msg_data")==null){
        alert("您的登录信息已过期请重新登陆");
        window.location.href="http://172.18.80.9:8087/admin/login/login"
    }
    var msg_data =JSON.parse(sessionStorage.getItem("msg_data"));
    console.log(msg_data.id);
    console.log(msg_data.token);
    var tid =location.search.split("add=")[1];
    console.log(tid);
    if(tid!=undefined){
        //专利类型
        $.ajax({
            type:"POST",
            data:{uid:msg_data.id,token:msg_data.token},
            url:"http://172.18.80.9:8087/admin/principal/sPatent",
            success:function(data){
                var msg=jQuery.parseJSON(data);
                if(msg.code==9){
                    alert(msg.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                var papertype=[];
                for(var i=0;i<msg.data.length;i++){
                    papertype.push(msg.data[i]);
                }
//                console.log(papertype);
                var paper=`<option>请选择专利类型</option>`;
                for(var d of papertype){
                    paper+=`<option value="${d.id}">${d.content}</option>`
                }
                $("#patenttype").html(paper);

            },
            error:function(err){
                console.log(err);
                alert("网络故障请检查");
            }
        });
        //请选择项目
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{uid:msg_data.id, token:msg_data.token,},
            url: "http://172.18.80.9:8087/admin/research/pName",
            success: function (data) {
                var html=`<option value="">请选择项目</option>`;
                for(var d of data.data){
                    html+=`<option value="${d.project_id+"?"+d.project_name}">${d.project_name}</option>
                    `
                }
                $("#project").html(html);
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    }
    //编辑
    var loc=location.href;
    var n1=loc.length;//地址的总长度
    var n2=loc.indexOf("id=");//取得=号的位置
    if(n2!=-1){
        var tid=decodeURI(loc.substr(n2+3, n1-n2));
        //专利类型**********************************
        $.ajax({
            type:"POST",
            data:{uid:msg_data.id,token:msg_data.token},
            url:"http://172.18.80.9:8087/admin/principal/sPatent",
            success:function(data){
                var msg=jQuery.parseJSON(data);
                if(msg.code==9){
                    alert(msg.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                var papertype=[];
                for(var i=0;i<msg.data.length;i++){
                    papertype.push(msg.data[i]);
                }
                var paper=`<option>请选择专利类型</option>`;
                for(var d of papertype){
                    console.log(d.id)
                    paper+=`<option value="${d.id}">${d.content}</option>`
                }
                $("#patenttype").html(paper);
                //详情
                $.ajax({
                    type:"POST",
                    data:{id:tid,uid:msg_data.id,token:msg_data.token},
                    dataType:"json",
                    url:"http://172.18.80.9:8087/admin/research/dpPatent",
                    success:function(data){
                        console.log(data.data);
                        for(d of data.data){
                            console.log(d.patent_id)
                            //项目
                            $("#project>option:first-child").css("display","none")
                            $("#project").val(d.project_id+"?"+d.project_name);
                            //专利类型
                            $("#patenttype>option:first-child").css("display","none")
                            $("#patenttype").val(d.patent_id);
                            //专利名称
                            $("#article_name").val(d.article_name);
                            //作者名称
                            $("#author").val(d.author)
                            //时间
                            var str=d.pilot_date;
                            if(str ==null){
                                console.log("时间为null");
                            }else{
                                arr=str.split(" ");
                                var pilot_date = arr[0];
                            }
                            $("#pilot_date").val(pilot_date)
                        }

                    },
                    error:function(err){
                        console.log(err);
                        alert("网络故障请检查");
                    }
                });

            },
            error:function(err){
                console.log(err);
                alert("网络故障请检查");
            }
        });
        //请选择项目
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{uid:msg_data.id, token:msg_data.token,},
            url: "http://172.18.80.9:8087/admin/research/pName",
            success: function (data) {
                var html=`<option value="">请选择项目</option>`;
                for(var d of data.data){
                    html+=`<option value="${d.project_id+"?"+d.project_name}">${d.project_name}</option>
                    `
                }
                $("#project").html(html);
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    }
    //保存
    $(document).on("click","#save",function () {
        //专利名称
        var article_name=$("#article_name").val();
        //作者
        var author=$("#author").val();
        //期刊名称
        var periodical_name=$("#periodical_name").val();
        //时间
        var pilot_date=$("#pilot_date").val();
        //项目id
        var  project_id=[];
        var  project_name=[];
        $("#project").each(function () {
            if($(this).val()==""){
                var b="";
                var c="";
            }
            if($(this).val()!=""){
                var b= $(this).val().split("?")[0];
                var c= $(this).val().split("?")[1];
            }
            project_id.push(b);
            project_name.push(c);

        })
        var projectid = project_id.join('');
        var projectname=project_name.join('');
        //专利类型id
        var patenttype=[];
        $("#patenttype").each(function(){
            if( $(this).val()=="请选择专利类型"){
                var a="";
            }else{
                var a= $(this).val();
            }
            patenttype.push(a);
        });
        var lid = patenttype.join('');
        console.log(lid)
        //上传文件
        formData = new FormData();
        if( $('#imageFile')[0].files[0]==undefined){
            formData.append('imageFile'," ");
        }else{
            formData.append('imageFile', $('#imageFile')[0].files[0]);
        }
        formData.append('uid', msg_data.id);
        formData.append('token', msg_data.token);
        formData.append('patent_id',lid);
        formData.append('id', tid);
        formData.append('article_name', article_name);
        formData.append('project_id', projectid);
        formData.append('author', author);
        formData.append('pilot_date', pilot_date);
        formData.append('project_name', projectname);
        $.ajax({
            type: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            url: "http://172.18.80.9:8087/admin/research/spPatent",
            success: function (data) {
                var data =JSON.parse(data)
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                if(data.code==2){
                    alert(data.msg)
                    console.log(data)
                }
                if(data.code==0){
                    alert(data.msg)
                    location.href="http://172.18.80.9:8087/admin/research/patentlist"
                }
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })
    //提交
    $(document).on("click","#submit",function () {
        //项目id
        var  project_id=[];
        var  project_name=[];
        $("#project").each(function () {
            if($(this).val()==""){
                var b="";
                var c="";
            }
            if($(this).val()!=""){
                var b= $(this).val().split("?")[0];
                var c= $(this).val().split("?")[1];
            }
            project_id.push(b);
            project_name.push(c);

        })
        var projectid = project_id.join('');
        var projectname=project_name.join('');
        if(projectname==""){
            alert("项目名称不能为空")
            return false;
        }
        //专利类型id
        var patenttype=[];
        $("#patenttype").each(function(){
            if( $(this).val()=="请选择专利类型"){
                var a="";
            }else{
                var a= $(this).val();
            }
            patenttype.push(a);
        });
        var lid = patenttype.join('');
        if(lid==""){
            alert("专利类型不能为空")
            return false;
        }
        //专利名称
        var article_name=$("#article_name").val();
        if(article_name==""){
            alert("专利名称不能为空")
            return false
        }
        //作者
        var author=$("#author").val();
        if(author==""){
            alert("作者不能为空")
            return false
        }
        //时间
        var pilot_date=$("#pilot_date").val();
        if(pilot_date==""){
            alert("时间不能为空")
            return false
        }
        //上传文件
        formData = new FormData();
        if( $('#imageFile')[0].files[0]==undefined){
            alert("上传文件不能为空")
            return false
        }
        formData.append('imageFile', $('#imageFile')[0].files[0]);
        formData.append('uid', msg_data.id);
        formData.append('token', msg_data.token);
        formData.append('patent_id',lid);
        formData.append('id', tid);
        formData.append('article_name', article_name);
        formData.append('project_id', projectid);
        formData.append('author', author);
        formData.append('pilot_date', pilot_date);
        formData.append('project_name', projectname);
        $.ajax({
            type: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            url: "http://172.18.80.9:8087/admin/research/bpPatent",
            success: function (data) {
                var data =JSON.parse(data)
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                if(data.code==2){
                    alert(data.msg)
                    console.log(data)
                }
                if(data.code==0){
                    if(confirm("提交后不可修改，您确定要提交吗")){
                        alert("提交成功");
                        window.location.href="http://172.18.80.9:8087/admin/research/patentlist"
                    }else{
                        return false;
                    }


                }
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })

})



